package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.MarketIssueExample;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.service.IssueService;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @auther: wubang
 * @description:
 * @date: 2022-06-27 14:12
 */
@Service
@Transactional
public class IssueServiceImpl implements IssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public CommonData<MarketIssue> selectIssueList(BaseParam baseParam, String question) {
        // 开启分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        // 创建example对象，添加准则
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();
        if (question != null) {
            criteria.andQuestionLike("%" + question + "%");
        }
        // 调用mapper查询数据库
        List<MarketIssue> issues = marketIssueMapper.selectByExample(marketIssueExample);
        // 获取分页信息
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(issues);
        // 封装数据返回结果
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketIssue insertIssue(String question, String answer) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setAnswer(answer);
        marketIssue.setQuestion(question);
        Date date = new Date(System.currentTimeMillis());
        marketIssue.setAddTime(date);
        marketIssue.setUpdateTime(date);
        marketIssueMapper.insertSelective(marketIssue);
        return marketIssue;
    }

    @Override
    public void deleteIssue(MarketIssue marketIssue) {
        marketIssueMapper.deleteByPrimaryKey(marketIssue.getId());
        return;
    }

    @Override
    public void updateIssue(MarketIssue marketIssue) {
        marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
    }
}
